// we7/pages/index/XSMSChooseAddressDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:"",
    address_id:"",
    block:0,
    StatusBar: app.globalData.StatusBar, //模态动画
    CustomBar: app.globalData.CustomBar,
    goods_id: "",
    goods_num: "",
    specification_id:"",
    false:false,
    true:true,
    address_tiao:true,
    shopcar_id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '收货地址',
    })
    var that = this
    if (options.shopcar_id){
      that.setData({
        shopcar_id: options.shopcar_id,
      })
    } else if (options.goods_id){
      that.setData({
        goods_id: options.goods_id,
        goods_num: options.goods_num,
        specification_id: options.specification_id
      })
    }
      const value = wx.getStorageSync('openid');

      app.util.request({
        url: 'admin/WxGoodsApi/addressList',
        data: {
          uid: value,
        },
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.code == 0) {
            that.setData({
              addressList: res.data.data
            })
            var assList = that.data.addressList
            for (var a = 0; a < assList.length; a++) {
              if (assList[a].status == 1) {
                that.setData({
                  block: a
                })
              }
            }
          }
        },
        fail: function () {
          wx.showToast({
            title: '服务器未响应',
            icon: 'none',
            duration: 2000
          })
        }
      })
    
  },
  
  //模态动画
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // 删除按钮
  delControl:function(e){
    var that=this
    var delindex = e.currentTarget.dataset.delindex;
    var address_id = that.data.addressList[delindex].id
    that.setData({
      address_id: address_id
    })
    this.showModal(e)
  },
  // 删除收货地址  携带的 that.data.address_id
  delSure:function(e){
    var that = this
    app.util.request({
      url: 'admin/WxGoodsApi/delAddress',
      data: {
        address_id: that.data.address_id,
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            that.onLoad({shopcar_id:0})
          }, 2000)
          
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器未响应',
          icon: 'none',
          duration: 2000
        })
      }
    })
    that.hideModal(e)

  },
  // 设为默认地址  红色  li_bottom_left_moRen
  setMoAddress:function(e){
    console.log(e);
    var that = this
    var mo = e.currentTarget.dataset.mo;
    var address_id = that.data.addressList[mo].id
    const value = wx.getStorageSync('openid');
    app.util.request({
      url: 'admin/WxGoodsApi/addressStatus',
      data: {
        address_id: address_id,
        uid: value
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            block: mo
          })
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          // setTimeout(function () {
          //   that.onLoad()
          // }, 2000)

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '服务器未响应',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})